package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.entity.MyCollections;
import com.leyou.cart.service.CartService;
import com.leyou.cart.service.CollectionsService;
import com.leyou.common.pojo.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CartController {

  @Resource private CartService cartService;

  @Resource private CollectionsService collectionsService;

  /**
   * 添加商品到购物车中
   *
   * @param cart 购物车对象接收参数
   * @return 没有返回值
   */
  @PostMapping
  public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
    cartService.addCart(cart);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

  /**
   * 获取购物车列表
   *
   * @return
   */
  @GetMapping("/list")
  public ResponseEntity<List<Cart>> findCartList() {
    List<Cart> cartList = cartService.findCartList();
    return ResponseEntity.ok(cartList);
  }

    /**
     * 批量添加购物车信息
     * @param cartList
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<Void> addCartList(@RequestBody List<Cart> cartList){
        cartService.addCartList(cartList);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 修改用户的购物车商品数量
     *
     * @param skuId skuId
     * @param num   商品数量
     * @return 无
     */
    @PutMapping
    public ResponseEntity<Void> updateCart(@RequestParam("id") Long skuId, @RequestParam("num") Integer num) {
        cartService.updateCart(skuId, num);
        return ResponseEntity.status(HttpStatus.OK).build();
    }



  /**
   * 添加收藏
   *
   * @param spuId 商品Id
   * @return 状态码
   */
  @PostMapping("/addMyCollections")
  public ResponseEntity<Void> addCollections(@RequestParam("id") Long spuId) {
    collectionsService.addCollections(spuId);
    return ResponseEntity.status(HttpStatus.OK).build();
  }

  /**
   * 查询用户的收藏
   *
   * @return 收藏对象集合
   */
  @GetMapping("/queryMyCollections")
  public ResponseEntity<PageResult<MyCollections>> queryMyCollections(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "rows", defaultValue = "5") Integer rows) {
    PageResult<MyCollections> pageResult = collectionsService.queryMyCollections(page, rows);
    return ResponseEntity.ok(pageResult);
  }

  /**
   * 取消收藏
   *
   * @param spuId 商品id
   * @return 你猜
   */
  @DeleteMapping("/deleteCollections/{spuId}")
  public ResponseEntity<Void> deleteCollections(@PathVariable("spuId") Long spuId) {
    collectionsService.deleteCollections(spuId);
    return ResponseEntity.status(HttpStatus.OK).build();
  }

  /*
   * 删除用户选中商品
   * xiaoChenChen写的代码
   * */
  @DeleteMapping
  public ResponseEntity<Void> deleteChooseSku(@RequestParam("ids") String ids)
  {
    cartService.deleteChooseSku(ids);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

  /*
   * 删除用户的购物车的商品
   * xiaoChenChen写的代码
   * */
  // @DeleteMapping("{skuId}")
  public ResponseEntity<Void> deleteSku(@PathVariable("skuId") Long skuId) {
    cartService.deleteSku(skuId);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

  /**
   * 根据skuId删除用户购物车商品
   *
   * @param skuId skuId
   * @return 无
   */
  @DeleteMapping("{skuId}")
  public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId) {
    cartService.deleteCart(skuId);
    return ResponseEntity.status(HttpStatus.OK).build();
  }
}
